import os
from PyQt5.QtGui import QIcon
from untitled import Ui_MainWindow
from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog, QMessageBox
import sys
import configparser

config = configparser.ConfigParser()

try:
    f = open("./config.ini", 'r')
    f.close()
except IOError:
    config["setting"] = {'path': '',
                         'number': 1
                         }
    with open('config.ini', 'w') as configfile:
        config.write(configfile)

config.read('config.ini')

vx_path = config['setting']["path"]
number = config['setting']["number"]

class mainwindow(Ui_MainWindow, QMainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.show()
        self.pushButton.clicked.connect(self.onButtonClick)
        self.pushButton_2.clicked.connect(self.onButtonClick_2)
        self.actiongy.triggered.connect(self.onMenuClick)
        self.lineEdit.setText(vx_path)
        self.spinBox.setValue(int(number))


    def onMenuClick(self):
        msg = "BUG 提交:<a href='https://gitee.com/kessel7/wechat-multi-opening-tool'>Gitee</a><br>更新:<a href='https://gitee.com/kessel7/wechat-multi-opening-tool/tree/master/%E6%88%90%E5%93%81'>查看</a><br>更多内容请关注微信公众号：救猿行动";
        QMessageBox.about(self, '关于', msg)

    def onButtonClick(self):
        # dir_choose = QFileDialog.getExistingDirectory(self, "选取文件夹")
        file_path, filetype = QFileDialog.getOpenFileName(self, '选择文件', './', "(*.*)")
        if file_path == '':
            return
        # 获取文件所在目录
        self.lineEdit.setText(os.path.dirname(file_path))

    def onButtonClick_2(self):
        config.set('setting', 'path', self.lineEdit.text())
        config.set('setting', 'number', str(self.spinBox.value()))
        with open('config.ini', 'w') as f:
            config.write(f)
        self.start(self.spinBox.value(), self.lineEdit.text())

    def start(self, c, p):
        d = 'start /d ' + '"' + p + '" ' + 'WeChat.exe'
        for i in range(c):
            os.popen(d)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = mainwindow()
    window.setWindowIcon(QIcon('ico.ico'))
    sys.exit(app.exec_())
